﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Biz_Dal.Biz;
using System.Collections;

public partial class Trang_web_Trang_chi_tiet : System.Web.UI.Page
{
    BizSach sach;
    int idS;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.QueryString["id"] != null)
        {
            int idsach = Convert.ToInt32(Request.QueryString["id"] as string);
            idS = idsach;
            sach=BizSach.getSachById(idsach);
            Load_anh();
            Ma_sach.Text = sach.maSach;
            Ten_sach.Text = sach.tenSach;
            Tac_gia.Text = sach.tacGia;
            Noi_dung.Text = sach.ndSach;
            don_gia.Text = sach.giaSach.ToString();
            LabelDonGia.Text = sach.giaSach.ToString("#,### VND");
            The_loai.Text = BizTheLoai.getTheLoaiByID(sach.idTheLoai).tenTheLoai;
            NXB.Text = BizNXB.getNXBByID(sach.idNXB).tenNXB;
        }
    }
    protected void ImageButtonMua_Click(object sender, ImageClickEventArgs e)
    {
        check_Number(txtCTSLMua.Text);


        //thêm vào giỏ hàng
        GIO_HANG giohang = new GIO_HANG();
        giohang.Id = idS;
        giohang.Ms = Ma_sach.Text;
        giohang.Ten_sach = Ten_sach.Text;
        giohang.Don_gia = double.Parse(don_gia.Text.ToString());
        giohang.So_luong = Convert.ToInt32(txtCTSLMua.Text.ToString());

        //kiểm tra và thêm vào giỏ hàng
        ArrayList mang = (ArrayList)Session["Gio_hang"];
        int ktra = kiem_tra_ton_tai(giohang.Id);
        if (ktra < 0)
        {
            mang.Add(giohang);
        }
        else
            ((GIO_HANG)mang[ktra]).So_luong += giohang.So_luong;
        Session["Gio_hang"] = mang;
        //tinh_tien();
        //tinh_so_sach();
    }



    int kiem_tra_ton_tai(int id)
    {
        ArrayList gio_hang = (ArrayList)Session["Gio_hang"];
        for (int i = 0; i <= gio_hang.Count - 1; i++)
            if (((GIO_HANG)gio_hang[i]).Id == id)
            {
                return i;
            }
        return -1;
    }

    void ShowMessage(string mTextMsg, string mControlFocus)
    {
        System.Text.StringBuilder sb = new System.Text.StringBuilder("");
        sb.Append("<script language=\"JavaScript\">");
        if (mTextMsg != "") sb.Append("  alert('" + mTextMsg + "');");
        if (mControlFocus != "") sb.Append("document.forms[0].item('" + mControlFocus + "').focus();");
        sb.Append("</script>");
        if (!Page.IsStartupScriptRegistered("setFocus"))
            Page.RegisterStartupScript("setFocus", sb.ToString());
    }

    //private int KiemTra_SoLuongDH(int idsp, List<BizCTDonDatHang> Giohang)
    //{
    //    if (Giohang != null)
    //    {
    //        for (int i = 0; i < Giohang.Count; i++)
    //        {
    //            BizCTDonDatHang ct = (BizCTDonDatHang)Giohang[i];
    //            if (ct.Sach.idSach == idsp)
    //                return i;
    //        }
    //    }
    //    return -1;
    //}

    private void Load_anh()
    {
        Image_sach.ImageUrl = "~/MyPhoto.ashx?ID=" + Request.QueryString["id"] as string;
    }


    protected void txtCTSLMua_TextChanged(object sender, EventArgs e)
    {
        check_Number(txtCTSLMua.Text);
    }

    public void check_Number(string str)
    {
        string s = "0123456789";
        char[] st = str.Trim().ToCharArray();
        for (int j = 0; j < st.Length; j++)
        {
            int f = s.IndexOf(st[j]);
            if (f == -1)
            {
                //Response.Write("<script>alert('Không phải kiểu số')</script>");
                txtCTSLMua.Text = "1";
                break;
            }
            else
            {
                if (int.Parse(str) > 3)
                    txtCTSLMua.Text = "3";
                else if(int.Parse(str)<=0)
                    txtCTSLMua.Text = "1";
            }
        }
    } 
}